package gazzera.swing.shares;

import gazzera.gui.messages.*;
import gazzera.swing.*;
import gazzera.swing.model.*;
import gazzera.utils.*;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;

import javax.swing.*;
import javax.swing.table.*;


public class ShareList extends JTable
{
	private List<Share> list = new ArrayList<Share>();
	private ShareListModel model = new ShareListModel(list);
	private JPopupMenu popupMenu = new JPopupMenu();
	
	private JMenuItem menuCancel;
	private final Gazzera gazzera;
	
	 public ShareList(Gazzera gazzera) 
	 {
		 
	        super();
	        this.gazzera = gazzera;
	        
	        setModel(model);
	        
	        setFillsViewportHeight(true);
	        setShowGrid(false);
	        
	        setDragEnabled(false);
	        setIntercellSpacing(new Dimension(0,0));
	        setCellSelectionEnabled(false);
	        setColumnSelectionAllowed(false);
	        setRowSelectionAllowed(true);
	        //setRowHeight(31);
	        
	        TableColumn col = getColumnModel().getColumn(0); 
	        //col.setCellRenderer(new DownloadIconRenderer(false));
	        col.setMaxWidth(28);
	        col.setMinWidth(28);
	        col.setResizable(false);
	        col.setHeaderValue("Id");
	        
	        col = getColumnModel().getColumn(1);
	        //col.setCellRenderer(new DownloadNameRenderer(false));
	        col.setHeaderValue("Folder");
	        col.setPreferredWidth(200);
	        
	        col = getColumnModel().getColumn(2);
	        //col.setCellRenderer(new ProgressRenderer());
	        col.setHeaderValue("Visible by");
	        col.setMaxWidth(300);
	        col.setPreferredWidth(200);
	        
	        
	        getTableHeader().setReorderingAllowed(false);
	        
	        
	        buildMenu();

	        
	        
	        addMouseListener(new MouseAdapter(){
	        	
	            public void mouseClicked(MouseEvent e){
	             if (e.getClickCount() == 2){
	                Share s = getSelectedShare();
	                AddShareFolderWindow a = new AddShareFolderWindow(ShareList.this.gazzera, s);
					a.setVisible(true);
	                }
	             }
	            public void mousePressed(MouseEvent e) {
	                showPopup(e);
	              }
	              public void mouseReleased(MouseEvent e) {
	                showPopup(e);
	              }
	              private void showPopup(MouseEvent e) {
	                if (e.isPopupTrigger()) {
	                  popupMenu(e.getComponent(), e.getX(), e.getY());
	                }
	              }

	            } );
	            
	 }
	public void updateShare(int id, String folder, List<String> groups)
	{
		if (Str.isEmpty(folder))
		{
			for (Share cc : list)
			{
				if (cc.id == id)
				{
					list.remove(cc);
					model.fireTableRowsDeleted(0, list.size());
					return;
				}
			}
		}
		else
		{
			for (Share cc : list)
			{
				if (cc.id == id)
				{
					cc.folder = folder;
					cc.groups = groups;
					model.fireTableRowsUpdated(0, list.size());
					return;
				}
			}
			list.add(new Share(id, folder, groups));
			model.fireTableRowsInserted(list.size(), list.size());
			this.repaint();
		}

		// list.add(s);
		// model.fireTableRowsInserted(list.size(), list.size());
	}
	 private void popupMenu(Component comp, int x, int y)
	    {
	    	if (this.getSelectedRowCount() > 0)
			{
	    		
	    		// Download c = getSelectedDownload();
	    		
	    		// check visibility of menu items
	    		 
	    		popupMenu.show(comp, x, y);
			}
	    	
	    }
	 private Share getSelectedShare()
		{
			int n = getSelectedRow();
			return (Share)model.getValueAt(n, -1);
		}
	 private void buildMenu()
		{
			menuCancel = new JMenuItem("Remove"); 
			
			menuCancel.addActionListener(new ActionListener()
			{
				@Override
				public void actionPerformed(ActionEvent e)
				{
					Share x = getSelectedShare();
					gazzera.gui.pass(new RemoveShare(x.id));
				}
			});

	        popupMenu.add(menuCancel);
		}

}
